package com.shou.service.impl;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.shou.CommonVar;
import com.shou.dao.SlideShowDao;
import com.shou.entity.SlideShow;
import com.shou.util.FileUtil;
import com.shou.util.ImageUtil;

@Service("slideShowService")
public class SlideShowService {

	@Autowired
	private SlideShowDao slideShowDaoImpl;
	
	
	public List<SlideShow> slideShowList() {
		return slideShowDaoImpl.findAll(SlideShow.class,"id",false);
	}
	
	public List<SlideShow> slideShowUsefulList() {
		//return slideShowDaoImpl.findAll(SlideShow.class,"id",false);
		return slideShowDaoImpl.getListBy(SlideShow.class,"useStatus",CommonVar.USE_ONUSE,"sort",true);
	}
	
	public SlideShow getSlideShowInfo(Integer slideId){
		return slideShowDaoImpl.get(SlideShow.class, slideId);
	}
	
	public void editSlideShow(SlideShow slideShow) {
		if(slideShow.getId() == null) {
			//新增
			slideShow.setCreateTime(new Date());
			slideShow.setUpdateTime(new Date());
			slideShowDaoImpl.saveOrUpdate(slideShow);
		}
		else {
			//更新
			SlideShow oldSlide = slideShowDaoImpl.get(SlideShow.class,slideShow.getId());
			oldSlide.setLinkContent(slideShow.getLinkContent());
			oldSlide.setLinkType(slideShow.getLinkType());
			oldSlide.setLinkUrl(slideShow.getLinkUrl());
			oldSlide.setPictureUrl(slideShow.getPictureUrl());
			oldSlide.setUseStatus(slideShow.getUseStatus());
			oldSlide.setUpdateTime(new Date());			
			slideShowDaoImpl.saveOrUpdate(oldSlide);
		}
	}
	
	public String uploadSlideImage(@RequestParam("cmfile") CommonsMultipartFile cmfile, Integer fsize) {
		
		String folder = "slideShow";
		String fileName = FileUtil.newFileName(cmfile.getOriginalFilename());
		String filePath = folder+"/"+fileName;
		ImageUtil.uploadPicture(cmfile, fsize,CommonVar.FILE_PREFIX+"/"+filePath);
		return filePath;
	}
}
